home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’96
/
Natural Order
/
Natural Order.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-21
|
3KB
|
76 lines
// Natural Order
// ©1996 Stuart Cheshire <cheshire@CS.Stanford.EDU>
//
// Natural Order patches IUMagString to make it sort strings containing numbers in a sensible order
// See Natural Order ReadMe for more details
// See TextUtils.h for the declaration of IUMagString etc.
#include <Traps.h>
#include "StuTypes.h"
void main(void);
pascal short my_pack6(const u_char *aPtr, const u_char *bPtr, short aLen, short bLen, short TrapSelector);
pascal short sys_pack6(const u_char *aPtr, const u_char *bPtr, short aLen, short bLen, short TrapSelector);
local pascal short StuMagString(const u_char *aPtr, const u_char *bPtr, short aLen, short bLen, short TrapSelector);
#define SysMagString(A,B,C,D) sys_pack6((A),(B),(C),(D),0x000A)
local void entry_point(void) /* no stack frame please ! */
{
asm {
@0 move.l a0, -(sp)
DetachResource
bra main
extern my_pack6: cmp.w #0x000A, 4(sp)
beq StuMagString
extern sys_pack6: jmp 0x12345678
}
}
#define isdigit(X) ((X) >= '0' && (X) <= '9')
local pascal short StuMagString(const u_char *aPtr, const u_char *bPtr, short aLen, short bLen, short TrapSelector)
{
short result;
const u_char *a = aPtr, *aEnd = a + aLen;
const u_char *b = bPtr, *bEnd = b + bLen;
while (aPtr < aEnd && bPtr < bEnd)
{
while (a < aEnd && b < bEnd && !(isdigit(*a) && isdigit(*b))) { a++; b++; }
result = SysMagString(aPtr, bPtr, a-aPtr, b-bPtr);
if (result) return (result);
aPtr = a;
bPtr = b;
while (a < aEnd && b < bEnd && isdigit(*a) && isdigit(*b)) { a++; b++; }
if (a < aEnd && isdigit(*a)) return(1);
if (b < bEnd && isdigit(*b)) return(-1);
}
return(SysMagString(aPtr, bPtr, a-aPtr, b-bPtr));
}
local Boolean TrapAvailable(u_long trap)
{
TrapType tType = (trap & 0x800 ? ToolTrap : OSTrap);
if (trap & 0x800) // if it is a ToolBox Trap
{
u_long n = 0x400; // number of toolbox traps
if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
n = 0x200;
if ((trap &= 0x7FF) >= n) trap = _Unimplemented;
}
return(NGetTrapAddress(trap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
}
typedef struct { WORD jmp; ProcPtr addr; } jmp;
#define TBpatch(VEC,T,NEW) p=&((jmp*)VEC)->addr; \
*p = GetToolTrapAddress(T); SetToolTrapAddress((ProcPtr)NEW,T)
#define OSpatch(VEC,T,NEW) p=&((jmp*)VEC)->addr; \
*p = GetOSTrapAddress(T); SetOSTrapAddress((ProcPtr)NEW,T)
export void main(void)
{
register ProcPtr *p;
TBpatch(sys_pack6, _Pack6, my_pack6);
if (TrapAvailable(_HWPriv)) FlushInstructionCache();
}